package com.ghck.shop.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ghck.shop.model.GoodsSpecification;

public class GoodsSpecificationMapperProvider {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(GoodsSpecificationMapperProvider.class);


	public String batchSaveGoodsSpecifications(Map<String, List<GoodsSpecification>> map){
		StringBuffer sb = new StringBuffer();
		List<GoodsSpecification> serviceCardItems = map.get("list");
		sb.append("insert into t_good_specification(good_id,market_price,sale_price,cost_price,amount,percentage_rate,name,`desc`,create_time,update_time) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "#'{'list[{0}].goodId},"
	    		+ "#'{'list[{0}].marketPrice},"
	    		+ "#'{'list[{0}].salePrice},"
	    		+ "#'{'list[{0}].costPrice},"
	    		+ "#'{'list[{0}].amount},"
	    		+ "#'{'list[{0}].percentageRate},"
	    		+ "#'{'list[{0}].name},"
	    		+ "#'{'list[{0}].desc},"
	    		+ "#'{'list[{0}].createTime},"
	    		+ "#'{'list[{0}].updateTime})");
		int len = serviceCardItems.size();
		for(int i=0;i<len;i++){
	    	sb.append(messageFormat.format(new Integer[]{i}));
            sb.append(",");
	    }
	    sb.setLength(sb.length() - 1);
	    LOGGER.info("sql:{}", sb.toString());
		return sb.toString();
	}
	
	public String deleteGoodsSpecificationByGoodIdAndNotIds(Map<String, Object> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = (List<Integer>)map.get("ids");
		MessageFormat messageFormat = new MessageFormat("#'{'ids[{0}]}");
		sb.append("update t_good_specification set status = -1 where good_id=#{goodId} and id not in ");
//		sb.append("delete from t_good_specification  where good_id=#{goodId} and id not in ");
		sb.append("(");
		int len = ids.size();
		for(int i=0;i<len;i++){
			sb.append(messageFormat.format(new Integer[]{i}));
			sb.append(",");
		}
		sb.setLength(sb.length() - 1);
		sb.append(")");
		return sb.toString();
	}
	
	
	public String update(GoodsSpecification goodsSpecification){
		StringBuffer sb = new StringBuffer();
		sb.append(" update t_good_specification set update_time = now()");
		if(goodsSpecification.getAmount() != null && goodsSpecification.getAmount() >= 0){
			sb.append(",amount=#{amount}");
		}
		if(goodsSpecification.getCostPrice() !=null && goodsSpecification.getCostPrice() >= 0){
			sb.append(",cost_price=#{costPrice}");
		}
		if(StringUtils.isNotEmpty(goodsSpecification.getDesc())){
			sb.append(",`desc`=#{desc}");
		}
		if(goodsSpecification.getMarketPrice() != null && goodsSpecification.getMarketPrice() >= 0){
			sb.append(",market_price=#{marketPrice}");
		}
		if(StringUtils.isNotEmpty(goodsSpecification.getName())){
			sb.append(",name=#{name}");
		}
		if(goodsSpecification.getPercentageRate() !=null && goodsSpecification.getPercentageRate() >=0 ){
			sb.append(",percentage_rate=#{percentageRate}");
		}
		if(goodsSpecification.getSalePrice() !=null && goodsSpecification.getSalePrice() >=0){
			sb.append(",sale_price=#{salePrice}");
		}
		sb.append(" where id=#{id}");
		return sb.toString();
	}
	
	public String findGoodsSpecificationsByGoodIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> goodIds = map.get("list");
		MessageFormat messageFormat = new MessageFormat("#'{'list[{0}]}");
		sb.append("select * from t_good_specification where  status=1 and good_id in");
		sb.append("(");
		int len = goodIds.size();
		for(int i=0;i<len;i++){
			sb.append(messageFormat.format(new Integer[]{i}));
			sb.append(",");
		}
		sb.setLength(sb.length() - 1);
		sb.append(")");
		return sb.toString();
	}
	
	public String findGoodsSpecificationsByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		MessageFormat messageFormat = new MessageFormat("#'{'list[{0}]}");
		sb.append("select * from t_good_specification where status=1 and id in");
		sb.append("(");
		int len = ids.size();
		for(int i=0;i<len;i++){
			sb.append(messageFormat.format(new Integer[]{i}));
			sb.append(",");
		}
		sb.setLength(sb.length() - 1);
		sb.append(")");
		return sb.toString();
	}
}
